package main

import (
	"testhttp/controller"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/accesslog"
	"github.com/kataras/iris/v12/middleware/logger"
)

func makeAccessLog() *accesslog.AccessLog {
	// Initialize a new access log middleware.
	ac := accesslog.File("./access.log")
	// Remove this line to disable logging to console:
	// ac.AddOutput(os.Stdout)

	// The default configuration:
	ac.Delim = '|'
	ac.TimeFormat = "2006-01-02 15:04:05"
	ac.Async = false
	ac.IP = true
	ac.BytesReceivedBody = true
	ac.BytesSentBody = true
	ac.BytesReceived = false
	ac.BytesSent = false
	ac.BodyMinify = true
	ac.RequestBody = true
	ac.ResponseBody = false
	ac.KeepMultiLineError = true
	ac.PanicLog = accesslog.LogHandler

	// Default line format if formatter is missing:
	// Time|Latency|Code|Method|Path|IP|Path Params Query Fields|Bytes Received|Bytes Sent|Request|Response|
	//
	// Set Custom Formatter:
	ac.SetFormatter(&accesslog.JSON{
		Indent:    "  ",
		HumanTime: true,
	})
	// ac.SetFormatter(&accesslog.CSV{})
	// ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})

	return ac
}

func main() {
	ac := makeAccessLog()
	defer ac.Close()
	app := iris.New()
	customLogger := logger.New(logger.Config{

		// IP显示请求的远程地址
		IP: true,
		//方法显示http方法
		Method: true,
		//状态显示状态代码
		Status: true,
		// Path显示请求路径
		Path: true,
		// Query将url查询附加到Path。
		Query: true,
		//Columns：true，
		// 如果不为空然后它的内容来自`ctx.Values(),Get("logger_message")
		//将添加到日志中。
		MessageContextKeys: []string{"logger_message", "request_body"},
		// MessageContextKeys: []string{"request_body"},
		//如果不为空然后它的内容来自`ctx.GetHeader（“User-Agent”）
		MessageHeaderKeys: []string{"User-Agent", "request_body"},
	})
	// app.Use(customLogger)

	app.OnAnyErrorCode(customLogger, func(ctx iris.Context) {
		//这应该被添加到日志中，因为`logger.Config＃MessageContextKey`
		ctx.Values().Set("logger_message",
			"a dynamic message passed to the logs")
		ctx.Writef("My Custom error page")
	})

	app.UseRouter(ac.Handler)
	// app.Use(controller.Sec)
	app.HandleDir("/", "./views")
	app.RegisterView(iris.HTML("./views", ".html"))

	app.Get("/", controller.Sec, controller.Index)
	app.Get("/captcha/*", controller.GetCaptchaImg)
	app.Get("/login.html", controller.GetCaptchaId)
	app.Get("/auth", controller.AuthUser)
	// app.Get("/login", controller.AuthUser)
	app.Get("/logout", controller.Logout)

	ordertempAPI := app.Party("/OrderTemp")

	{
		ordertempAPI.Use(iris.Compression)
		ordertempAPI.Use(controller.Sec)
		ordertempAPI.Get("/list", controller.ListTemp)
		ordertempAPI.Get("/view", controller.Viewtemp)
		ordertempAPI.Post("/del", controller.Deltemp)
		ordertempAPI.Post("/add", controller.Addtemp)
		ordertempAPI.Get("/listforselect", controller.TempListforSelect)
	}

	rulesApi := app.Party("/MappingRules")

	{
		rulesApi.Use(iris.Compression)
		rulesApi.Use(controller.Sec)
		rulesApi.Get("/list", controller.ListRules)
		rulesApi.Get("/view", controller.ViewRules)
		rulesApi.Post("/del", controller.DelRules)
		rulesApi.Post("/add", controller.AddRules)
		rulesApi.Post("/update", controller.UpdateRules)
	}

	memberAPI := app.Party("/Member")

	{
		memberAPI.Use(iris.Compression)
		memberAPI.Use(controller.Sec)
		memberAPI.Post("/list", controller.ListMember)
		// memberAPI.Get("/search", controller.Search)
		memberAPI.Post("/del", controller.DelMember)
		memberAPI.Post("/add", controller.AddMember)
		memberAPI.Get("/view", controller.ViewMember)
		memberAPI.Post("/update", controller.UpdateMember)
	}
	goodsAPI := app.Party("/Goods")
	{
		goodsAPI.Use(iris.Compression)
		goodsAPI.Use(controller.Sec)
		goodsAPI.Post("/list", controller.ListGoods)
		// memberAPI.Get("/search", controller.Search)
		goodsAPI.Post("/del", controller.DelGoods)
		goodsAPI.Post("/add", controller.AddGoods)
		goodsAPI.Get("/view", controller.GetGoods)
		goodsAPI.Post("/update", controller.UpdateGoods)
	}

	dealerAPI := app.Party("/Dealer")
	{
		dealerAPI.Use(iris.Compression)
		dealerAPI.Use(controller.Sec)
		dealerAPI.Post("/list", controller.ListDealer)
		// memberAPI.Get("/search", controller.Search)
		dealerAPI.Post("/del", controller.DelDealer)
		dealerAPI.Post("/add", controller.AddDealer)
		dealerAPI.Get("/view", controller.ViewDealer)
		dealerAPI.Post("/update", controller.UpdateDealer)
	}

	orderAPI := app.Party("/Order")
	{
		orderAPI.Use(iris.Compression)
		orderAPI.Use(controller.Sec)
		orderAPI.Post("/list", controller.ListOrder)
		// memberAPI.Get("/search", controller.Search)
		orderAPI.Post("/del", controller.DelOrder)
		orderAPI.Post("/add", controller.AddOrder)
		orderAPI.Get("/view", controller.ViewOrder)
		orderAPI.Post("/update", controller.Updatev)
	}

	testAPI := app.Party("/Test")
	{
		testAPI.Use(iris.Compression)

		testAPI.Post("/edit", controller.TestP)
		testAPI.Get("/edit", controller.TestG)

	}

	app.Run(iris.Addr(":8080"))
}
